package com.example.web.controller;

import com.example.web.entity.SequenceConfig;
import com.example.web.entity.Result;
import com.example.web.service.SequenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/api/sequence")
@CrossOrigin(origins = "http://localhost:3001", allowCredentials = "true")
public class SequenceController {
    
    @Autowired
    private SequenceService sequenceService;
    
    /**
     * 分页查询序列号配置列表
     */
    @GetMapping("/config/list")
    public Result getConfigList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String code) {
        try {
            Map<String, Object> data = sequenceService.getConfigList(page, size, name, code);
            return Result.success(data);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 根据ID查询配置
     */
    @GetMapping("/config/{id}")
    public Result getConfig(@PathVariable String id) {
        try {
            SequenceConfig config = sequenceService.findById(id);
            if (config == null) {
                return Result.error("配置不存在");
            }
            return Result.success(config);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 创建配置
     */
    @PostMapping("/config")
    public Result createConfig(@Valid @RequestBody SequenceConfig config) {
        try {
            SequenceConfig created = sequenceService.createConfig(config);
            return Result.success(created);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新配置
     */
    @PutMapping("/config/{id}")
    public Result updateConfig(@PathVariable String id, @Valid @RequestBody SequenceConfig config) {
        try {
            config.setId(id);
            SequenceConfig updated = sequenceService.updateConfig(config);
            return Result.success(updated);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除配置
     */
    @DeleteMapping("/config/{id}")
    public Result deleteConfig(@PathVariable String id) {
        try {
            boolean success = sequenceService.deleteConfig(id);
            if (success) {
                return Result.success("删除成功");
            }
            return Result.error("删除失败");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 生成序列号
     */
    @PostMapping("/generate")
    public Result generateSequence(@RequestParam String code) {
        try {
            String sequence = sequenceService.generateSequence(code);
            return Result.success(sequence);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
